package servlet.rool;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import pojo.Menu;
import pojo.Role;
import service.MenuService;
import service.RoleService;
import service.impl.MenuServiceImpl;
import service.impl.RoleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 修改角色表前回显的servlet
 */
@WebServlet(value = "/RoleEchoServlet")
public class RoleEchoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String id=request.getParameter("ID");
        RoleService rs=new RoleServiceImpl();
        Role role=rs.queryRoleDesc(Integer.parseInt(id));
        MenuService ms=new MenuServiceImpl();
        List<Menu> menus=ms.queryMenu();
        List<Menu> menusById=ms.queryMenuByRoId(Integer.parseInt(id));
        request.setAttribute("menusById",menusById);
        request.setAttribute("role",role);
        request.setAttribute("menus",menus);

        //转换JSON格式
        JSONObject json=new JSONObject();
        json.put("menus", JSONArray.fromObject(menus));
        request.setAttribute("jsonmenus",json);
        JSONObject json2=new JSONObject();
        json2.put("menusById",JSONArray.fromObject(menusById));
        request.setAttribute("jsonmenusById",json2);

        request.getRequestDispatcher("jsp/yjj/RoleUpdate.jsp").forward(request,response);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        doGet(request, response);
    }
}
